<script lang="ts">
export default {
  name: 'productList'
};
</script>

<script setup lang="ts">
import router from '@/router';
// import ProductEdit from './components/productEdit.vue'
import { formatMillisecondsToDate } from '@/utils/utils'
import {
  getProductList,
  deleteProduct,
} from '@/api/product';
import { getDictPage } from '@/api/dict'


const queryFormRef = ref(ElForm); // 查询表单
const formRef = ref(ElForm); // 用户表单
const defaultTime: [Date, Date] = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
];
const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams: any = reactive({
  current: 1,
  pageSize: 9999,
  beginTime: '',
  endTime: '',
  productId: '',
  name: '',
  description: ''
});

const formData = reactive({
  status: 1,

});

const dialog = reactive<any>({
  visible: false,
});

const dataList = ref([]);
const statusList = ref<any>([]);
/**
 * 查询
 */
function handleQuery() {
  getProductList(queryParams)
    .then((res: any) => {

      dataList.value = res.data.data;
      // total.value = res.data.total;
    })
}
/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.current = 1;
  handleQuery();
}


/**
 * 行checkbox change事件
 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}


/**
 * 重置表单
 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
}


/**
 * 类型表单弹窗
 *
 */
function openDialog(row?: object) {
  dialog.visible = true;
  if (row) {
    dialog.title = '修改产品';
    Object.assign(formData, row);
  } else {
    //resetForm();
    dialog.title = '新增产品';
  }
}

/**
 * 删除
 */
function handleDelete(id?: number) {
  let idList = ids.value
  id ? idList = [...ids.value, id] : null
  if (idList.length === 0) {
    ElMessage.warning('请勾选删除项');
    return;
  }

  ElMessageBox.confirm('确认删除已选中的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteProduct({ ids: idList }).then(() => {
      ElMessage.success('删除成功');
      handleQuery();
    });
  });
}

function changeSwitch(row: any) {
  // pptSwitch({
  //   ...row
  // }).then((res: any) => {
  //   ElMessage.success('操作成功');
  // })
}

function goDetail() {
  router.push({ path: '/productDetail' })
}

function getDict() {
  getDictPage({
    current: 1,
    pageSize: 9999,
    parentId: 160
  }).then((res) => {
    statusList.value = res.data.data
  })
}

onMounted(async () => {
  getDict()
  handleQuery(); // 初始化用户列表数据
});

watch(
  () => queryParams.startEndTime,
  (val: any) => {
    if (Array.isArray(val)) {
      queryParams.beginTime = val[0];
      queryParams.endTime = val[1];
    } else {
      queryParams.beginTime = '';
      queryParams.endTime = '';
    }
  }
);


</script>

<template>
  <div class="app-container">


    <div class="search">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">

        <el-form-item prop="name" label="产品名称">
          <el-input v-model="queryParams.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />搜索
          </el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <template #header>
        <div class="flex justify-between">
          <div>
            <el-button type="success" @click="goDetail()">
              <i-ep-plus />新增
            </el-button>
            <el-button type="danger" :disabled="ids.length === 0" @click="handleDelete()">
              <i-ep-delete />删除
            </el-button>
          </div>
        </div>
      </template>

      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" width="55" />
        <el-table-column label="商品名称" align="center" prop="productId" />
        <el-table-column label="商品图片" align="center" prop="name" />
        <el-table-column label="价格/货号" align="center" prop="metadata" />
        <el-table-column label="排序" align="center" prop="productType" />
        <el-table-column label="SKU库存" align="center" prop="driverId" />
        <el-table-column label="销量" align="center" prop="protocolPluginId" />
        <el-table-column label="审核状态" align="center" prop="protocolTransport" />
        <el-table-column label="操作" fixed="right" align="center" width="200">
          <template #default="scope">
            <el-button type="primary" link size="small" @click.stop="openDialog(scope.row)">
              <i-ep-edit />编辑
            </el-button>
            <el-button type="primary" link size="small" @click.stop="handleDelete(scope.row.id)">
              <i-ep-delete />删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

  </div>
</template>
<style lang="scss" scoped>
.app-container {
  .mb-4 {
    .data-box {
      background: white;
      height: 130px;
      display: flex;
      justify-content: space-between;
      padding: 20px;

      .data-box-info {
        .data-box-info-value {
          text-align: center;
        }

        .data-box-info-name {
          text-align: center;
        }

        .data-box-info-title {
          font-size: 20px;
          font-weight: bold;
          margin-bottom: 10px;
        }
      }
    }
  }
}
</style>